/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hvtt.web.controller;

import com.hvtt.web.connection.Command;
import com.hvtt.web.connection.WEBConnection;
import com.hvtt.web.connection.WEBParamCollection;
import com.hvtt.web.models.RPT_TongDoanhThuTheoGameModel;
import com.hvtt.web.utilities.Utilities;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.StreamedContent;
import org.primefaces.model.TreeNode;
import org.primefaces.model.chart.CartesianChartModel;
import org.primefaces.model.chart.LineChartSeries;
import org.primefaces.model.chart.PieChartModel;

/**
 *
 * @author Administrator
 */
@ManagedBean
@SessionScoped
public class RPT_TongDoanhThuTheoGameController  implements Serializable {
    
     private WEBConnection DAL = WEBConnection.getInstance();
      ReportEcxelController controller=new ReportEcxelController();
    private StreamedContent fileExporter;
       private String cpid = "";
    private String hdh = "";
    private String gameID = "";
    private String congTT = "";
    private String telco = "";
    private String loaiThongKe = "";
    private Date tuNgay = Calendar.getInstance().getTime();
    private Date denNgay = new java.sql.Date(0);
    private long giaTriTheNap = 0;
    private String page = "";
    private String loaiBD = "";
    
    private long tong1=0;
    private long tong2=0;
    private long tong3=0;
    private long tong4=0;
    private long tong5=0;
    
    public String getPage() {
        return page;
    }

    public void setPage(String page) {
        this.page = page;
    }

    /**
     * @return the cpid
     */
    public String getCpid() {
        return cpid;
    }

    /**
     * @param cpid the cpid to set
     */
    public void setCpid(String cpid) {
        this.cpid = cpid;
    }

    /**
     * @return the hdh
     */
    public String getHdh() {
        return hdh;
    }

    /**
     * @param hdh the hdh to set
     */
    public void setHdh(String hdh) {
        this.hdh = hdh;
    }

    /**
     * @return the gameID
     */
    public String getGameID() {
        return gameID;
    }

    /**
     * @param gameID the gameID to set
     */
    public void setGameID(String gameID) {
        this.gameID = gameID;
    }

    /**
     * @return the congTT
     */
    public String getCongTT() {
        return congTT;
    }

    /**
     * @param congTT the congTT to set
     */
    public void setCongTT(String congTT) {
        this.congTT = congTT;
    }

    /**
     * @return the telco
     */
    public String getTelco() {
        return telco;
    }

    /**
     * @param telco the telco to set
     */
    public void setTelco(String telco) {
        this.telco = telco;
    }

    /**
     * @return the loaiThongKe
     */
    public String getLoaiThongKe() {
        return loaiThongKe;
    }

    /**
     * @param loaiThongKe the loaiThongKe to set
     */
    public void setLoaiThongKe(String loaiThongKe) {
        this.loaiThongKe = loaiThongKe;
    }

    /**
     * @return the tuNgay
     */
    public Date getTuNgay() {
        return tuNgay;
    }

    /**
     * @param tuNgay the tuNgay to set
     */
    public void setTuNgay(Date tuNgay) {
        this.tuNgay = tuNgay;
    }

    /**
     * @return the denNgay
     */
    public Date getDenNgay() {
        return denNgay;
    }

    /**
     * @param denNgay the denNgay to set
     */
    public void setDenNgay(Date denNgay) {
        this.denNgay = denNgay;
    }

    /**
     * @return the giaTriTheNap
     */
    public long getGiaTriTheNap() {
        return giaTriTheNap;
    }

    /**
     * @param giaTriTheNap the giaTriTheNap to set
     */
    public void setGiaTriTheNap(long giaTriTheNap) {
        this.giaTriTheNap = giaTriTheNap;
    }

    /**
     * @return the loaiBD
     */
    public String getLoaiBD() {
        return loaiBD;
    }

    /**
     * @param loaiBD the loaiBD to set
     */
    public void setLoaiBD(String loaiBD) {
        this.loaiBD = loaiBD;
    }

    /**
     * @return the tong1
     */
    public long getTong1() {
        return tong1;
    }

    /**
     * @param tong1 the tong1 to set
     */
    public void setTong1(long tong1) {
        this.tong1 = tong1;
    }

    /**
     * @return the tong2
     */
    public long getTong2() {
        return tong2;
    }

    /**
     * @param tong2 the tong2 to set
     */
    public void setTong2(long tong2) {
        this.tong2 = tong2;
    }

    /**
     * @return the tong3
     */
    public long getTong3() {
        return tong3;
    }

    /**
     * @param tong3 the tong3 to set
     */
    public void setTong3(long tong3) {
        this.tong3 = tong3;
    }

    /**
     * @return the tong4
     */
    public long getTong4() {
        return tong4;
    }

    /**
     * @param tong4 the tong4 to set
     */
    public void setTong4(long tong4) {
        this.tong4 = tong4;
    }
    
    
    public TreeNode RPT_ThongKeDoanhThu_BAC_ThongKe() throws SQLException {
        TreeNode root = new DefaultTreeNode(new RPT_TongDoanhThuTheoGameModel(), null);
        WEBParamCollection Params = new WEBParamCollection();
        Params.add(("-1".equals(this.getCpid()) ? "" : this.getCpid()));
        Params.add(("-1".equals(this.getGameID()) ? "" : this.getGameID()));
        Params.add(("-1".equals(this.getLoaiThongKe()) ? "" : this.getLoaiThongKe()));
        Params.add(this.getTuNgay());
        Params.add(this.getDenNgay());
        List<RPT_TongDoanhThuTheoGameModel> lst = DAL.executeQuery("{call RPT_TongDoanhThu(?,?,?,?,?)}", Command.sqlStoredProcedure, Params, RPT_TongDoanhThuTheoGameModel.class);

        int i = 0;
        long lTong1=0;
        long lTong2=0;
        long lTong3=0;
        
        String strCPID;
        while (i < lst.size()) {
            RPT_TongDoanhThuTheoGameModel m = new RPT_TongDoanhThuTheoGameModel();
            m.setCpName(lst.get(i).getCpName());
            TreeNode noteParent = new DefaultTreeNode(m, root);
            noteParent.setExpanded(true);


            strCPID = lst.get(i).getCpid();

            int iCountSoUser = 0;
            long lCountSoBIT = 0;
            long lPT=0;

            if (i < lst.size()) {
                while (i < lst.size() && (lst.get(i).getCpid() == null ? strCPID == null : lst.get(i).getCpid().equals(strCPID))) {
                    lst.get(i).setCpName("");
                    lst.get(i).setPT(lst.get(i).getTongSoBit() * 20 / 100);
                    noteParent.getChildren().add(new DefaultTreeNode(lst.get(i)));
                    iCountSoUser += lst.get(i).getSoNguoiChoi();
                    lCountSoBIT += lst.get(i).getTongSoBit();
                    lTong1+=lst.get(i).getSoNguoiChoi();
                    lTong2+=lst.get(i).getTongSoBit();
                    lTong3+=lst.get(i).getPT();
                    lPT+=lst.get(i).getPT();
                 //   m.setGame(lst.get(i).getGame());
                    i++;
                }
                m.setSoNguoiChoi(iCountSoUser);
                m.setTongSoBit(lCountSoBIT);
                m.setPT(lPT);
              //  m.setGame("");

            }
            // i++;
        }
        this.setTong1(lTong1);
        this.setTong2(lTong2);
        this.setTong3(lTong3);
        return root;
    }

    public TreeNode RPT_ThongKeDoanhThu_BIT_ThongKe() throws SQLException {
        TreeNode root = new DefaultTreeNode(new RPT_TongDoanhThuTheoGameModel(), null);
        WEBParamCollection Params = new WEBParamCollection();
        Params.add(("-1".equals(this.getCpid()) ? "" : this.getCpid()));
        Params.add(("-1".equals(this.getGameID()) ? "" : this.getGameID()));
        Params.add(("-1".equals(this.getLoaiThongKe()) ? "" : this.getLoaiThongKe()));
        Params.add(this.getTuNgay());
        Params.add(this.getDenNgay());
        List<RPT_TongDoanhThuTheoGameModel> lst = DAL.executeQuery("{call RPT_TongDoanhThu(?,?,?,?,?)}", Command.sqlStoredProcedure, Params, RPT_TongDoanhThuTheoGameModel.class);
        long lTong1=0;
        long lTong2=0;
        long lTong3=0;
        
        int i = 0;
        String strCPID;
        while (i < lst.size()) {
            RPT_TongDoanhThuTheoGameModel m = new RPT_TongDoanhThuTheoGameModel();
            m.setCpName(lst.get(i).getCpName());
            TreeNode noteParent = new DefaultTreeNode(m, root);
            noteParent.setExpanded(true);


            strCPID = lst.get(i).getCpid();

            int iCountSoUser = 0;
            long lCountSoBIT = 0;

            if (i < lst.size()) {
                while (i < lst.size() && (lst.get(i).getCpid() == null ? strCPID == null : lst.get(i).getCpid().equals(strCPID))) {
                    lst.get(i).setCpName("");
                    lst.get(i).setPT(lst.get(i).getTongSoBit() * 2 / 100);
                    noteParent.getChildren().add(new DefaultTreeNode(lst.get(i)));
                    iCountSoUser += lst.get(i).getSoNguoiChoi();
                    lCountSoBIT += lst.get(i).getTongSoBit();
                    
                    lTong1+=lst.get(i).getSoNguoiChoi();
                    lTong2+=lst.get(i).getTongSoBit();
                    lTong3+=lst.get(i).getTongSoBit() * 2 / 100;
                    //m.setGame(lst.get(i).getGame());
                    i++;
                }
                m.setSoNguoiChoi(iCountSoUser);
                m.setTongSoBit(lCountSoBIT);
                m.setPT(lCountSoBIT * 2 / 100);

            }
            // i++;
        }
        this.setTong1(lTong1);
        this.setTong2(lTong2);
        this.setTong3(lTong3);
        return root;
    }
    
    public TreeNode RPT_ThongKeDoanhThuTheoGame_ThongKe() throws SQLException {
        TreeNode root = new DefaultTreeNode(new RPT_TongDoanhThuTheoGameModel(), null);
        WEBParamCollection Params = new WEBParamCollection();
        Params.add(("-1".equals(this.getCpid()) ? "" : this.getCpid()));
        Params.add(("-1".equals(this.getGameID()) ? "" : this.getGameID()));
        Params.add(this.getTuNgay());
        Params.add(this.getDenNgay());
        List<RPT_TongDoanhThuTheoGameModel> lst = DAL.executeQuery("{call RPT_TongDoanhThuTheoGame(?,?,?,?)}", Command.sqlStoredProcedure, Params, RPT_TongDoanhThuTheoGameModel.class);
        long lTong1=0;
        long lTong2=0;
        long lTong3=0;
        long lTong4=0;
        long lTong5=0;
        
        int i = 0;
        String strCPID;
        while (i < lst.size()) {
            RPT_TongDoanhThuTheoGameModel m = new RPT_TongDoanhThuTheoGameModel();
            m.setCpName(lst.get(i).getCpName());
            TreeNode noteParent = new DefaultTreeNode(m, root);
            noteParent.setExpanded(true);


            strCPID = lst.get(i).getCpid();

            int iCountSoUser = 0;
            long lCountSoBIT = 0;
            
            int iCountSoUserLogin = 0;
            double iARPU=0;
            if (i < lst.size()) {
                while (i < lst.size() && (lst.get(i).getCpid() == null ? strCPID == null : lst.get(i).getCpid().equals(strCPID))) {
                    lst.get(i).setCpName(lst.get(i).getGameName());
                    lst.get(i).setPT(lst.get(i).getTongSoBit() * 2 / 100);
                    lst.get(i).setArpu((lst.get(i).getTongSoBit() * 2 / 100)/(lst.get(i).getSoNguoiChoiDistinct()<=0?1:lst.get(i).getSoNguoiChoiDistinct()));
                    
                    noteParent.getChildren().add(new DefaultTreeNode(lst.get(i)));
                    iCountSoUser += lst.get(i).getSoNguoiChoi();
                    lCountSoBIT += lst.get(i).getTongSoBit();
                    
                    lTong1+=lst.get(i).getSoNguoiChoi();
                    lTong2+=lst.get(i).getTongSoBit();
                    lTong3+=lst.get(i).getTongSoBit() * 2 / 100;
                    lTong4+=(lst.get(i).getTongSoBit() * 2 / 100);
                    lTong5=lst.get(i).getTongSoNguoiDistinct();
                    
                    iARPU+=(lst.get(i).getTongSoBit() * 2 / 100)/(lst.get(i).getSoNguoiChoiDistinct()<=0?1:lst.get(i).getSoNguoiChoiDistinct());
                    iCountSoUserLogin+=lst.get(i).getSoNguoiChoiDistinct();
                    //m.setGame(lst.get(i).getGame());
                    i++;
                }
                m.setSoNguoiChoi(iCountSoUser);
                m.setTongSoBit(lCountSoBIT);
                m.setPT(lCountSoBIT * 2 / 100);
                m.setArpu(iARPU);
                m.setSoNguoiChoiDistinct(iCountSoUserLogin);

            }
            // i++;
        }
        this.setTong1(lTong1);
        this.setTong2(lTong2);
        this.setTong3(lTong3);
        this.setTong4(lTong4/(lTong5<=0?1:lTong5));
        this.setTong5(lTong5);
        return root;
    }
    
    
    

    public PieChartModel RPT_ThongKeDoanhThu_Chart() throws SQLException {
        PieChartModel c = new PieChartModel();
        List<RPT_TongDoanhThuTheoGameModel> lst;

        
        // taikhoans.setLabel("X: ngày, Y: số tài khoản");
        //ChartSeries hdhs = new ChartSeries();

        WEBParamCollection Params = new WEBParamCollection();
        Params.add(("-1".equals(this.getCpid()) ? "" : this.getCpid()));
        Params.add(("-1".equals(this.getGameID()) ? "" : this.getGameID()));
        Params.add(this.getTuNgay());
        Params.add(this.getDenNgay());
        lst = DAL.executeQuery("{call RPT_TongDoanhThuTheoGame_Chart(?,?,?,?)}", Command.sqlStoredProcedure, Params, RPT_TongDoanhThuTheoGameModel.class);


        for (RPT_TongDoanhThuTheoGameModel m : lst) {
           c.set(m.getGameName()+ " "+ Utilities.formatNumber("#,###", m.getTongSoBit()*2/100), m.getTongSoBit()*2/100);
        }
        return c;
    }
    public PieChartModel RPT_ThongKeDoanhThu_Chart_TheoCP0() throws SQLException {
        PieChartModel c = new PieChartModel();
        List<RPT_TongDoanhThuTheoGameModel> lst;

        
        // taikhoans.setLabel("X: ngày, Y: số tài khoản");
        //ChartSeries hdhs = new ChartSeries();

        WEBParamCollection Params = new WEBParamCollection();
        Params.add(("-1".equals(this.getCpid()) ? "" : this.getCpid()));
        Params.add(("-1".equals(this.getGameID()) ? "" : this.getGameID()));
        Params.add(this.getTuNgay());
        Params.add(this.getDenNgay());
        lst = DAL.executeQuery("{call RPT_TongDoanhThuTheoGame_Chart_CP0(?,?,?,?)}", Command.sqlStoredProcedure, Params, RPT_TongDoanhThuTheoGameModel.class);


        for (RPT_TongDoanhThuTheoGameModel m : lst) {
           c.set(m.getGameName()+" "+ Utilities.formatNumber("#,###", m.getTongSoBit()*2/100), m.getTongSoBit()*2/100);
        }
        return c;
    }
  
    
    public CartesianChartModel RPT_ThongKeDoanhThu_Chart_Game_line_ngay() throws SQLException {
        CartesianChartModel c = new CartesianChartModel();
       
        
        List<RPT_TongDoanhThuTheoGameModel> lst;

   

        WEBParamCollection Params = new WEBParamCollection();
        Params.add(("-1".equals(this.getCpid()) ? "" : this.getCpid()));
        Params.add(("-1".equals(this.getGameID()) ? "" : this.getGameID()));
        Params.add(this.getTuNgay());
        Params.add(this.getDenNgay());
        lst = DAL.executeQuery("{call RPT_TongDoanhThuTheoGame_Chart_line_ngay(?,?,?,?)}", Command.sqlStoredProcedure, Params, RPT_TongDoanhThuTheoGameModel.class);

        if(Sys_NguoiDungController.findUser().isIsAdmin())
        {
            List<RPT_TongDoanhThuTheoGameModel> lst1=DAL.executeQuery("Select distinct GameInfo.GameID,GameInfo.GameName from GameInfo order by GameInfo.GameID desc",RPT_TongDoanhThuTheoGameModel.class);
            for(RPT_TongDoanhThuTheoGameModel m:lst1)
            {
                LineChartSeries l=new LineChartSeries();
                l.setLabel("Game " + m.getGameName());
                
                int i=0;
                boolean bln=false;
                
                //ChartSeries data=new ChartSeries();
                
                while(i<lst.size())
                {
                    if(!l.getData().containsKey(Utilities.parseDateTime(lst.get(i).getNgay(), "dd/MM")))
                    {
                        l.set(Utilities.parseDateTime(lst.get(i).getNgay(), "dd/MM"), 0);
                        l.setShowLine(true);
                    }
                    if(lst.get(i).getGameID()== null ? m.getGameID()== null : lst.get(i).getGameID().equals(m.getGameID()))
                    {
                        if(lst.get(i).getTongSoBit()>0)
                        {
                            double iARPU=(lst.get(i).getTongSoBit() * 2 / 100)/(lst.get(i).getSoNguoiChoiDistinct()<=0?1:lst.get(i).getSoNguoiChoiDistinct());
                            l.set(Utilities.parseDateTime(lst.get(i).getNgay(), "dd/MM"), iARPU);
                            bln=true;
                            //l.setShowLine(true);
                        }
                    }
                   
                        
                    i++;
                }
                if(bln)
                    c.addSeries(l);
            }
            
           
        }
        else
        {
           
            LineChartSeries l2=new LineChartSeries();
            l2.setLabel("Tổng số tiền nạp theo CP");

            for (RPT_TongDoanhThuTheoGameModel m : lst) {
               
                l2.set(Utilities.parseDateTime(m.getNgay(), "dd/MM"),m.getTongSoBit());
                l2.setShowLine(true);    

            }
             
             c.addSeries(l2);
        }
        
        return c;
    }
     public CartesianChartModel RPT_ThongKeDoanhThu_Chart_Game_line_thang() throws SQLException {
       CartesianChartModel c = new CartesianChartModel();
       
        
        List<RPT_TongDoanhThuTheoGameModel> lst;

   

        WEBParamCollection Params = new WEBParamCollection();
        Params.add(("-1".equals(this.getCpid()) ? "" : this.getCpid()));
        Params.add(("-1".equals(this.getGameID()) ? "" : this.getGameID()));
        Params.add(this.getTuNgay());
        Params.add(this.getDenNgay());
        lst = DAL.executeQuery("{call RPT_TongDoanhThuTheoGame_Chart_line_thang(?,?,?,?)}", Command.sqlStoredProcedure, Params, RPT_TongDoanhThuTheoGameModel.class);

        if(Sys_NguoiDungController.findUser().isIsAdmin())
        {
            List<RPT_TongDoanhThuTheoGameModel> lst1=DAL.executeQuery("Select distinct GameInfo.GameID,GameInfo.GameName from GameInfo order by GameInfo.GameID desc",RPT_TongDoanhThuTheoGameModel.class);
            for(RPT_TongDoanhThuTheoGameModel m:lst1)
            {
                LineChartSeries l=new LineChartSeries();
                l.setLabel("Game " + m.getGameName());
                
                int i=0;
                boolean bln=false;
                
                //ChartSeries data=new ChartSeries();
                
                while(i<lst.size())
                {
                    if(!l.getData().containsKey(lst.get(i).getThang()))
                    {
                        l.set(lst.get(i).getThang(), 0);
                        l.setShowLine(true);
                    }
                    if(lst.get(i).getGameID()== null ? m.getGameID()== null : lst.get(i).getGameID().equals(m.getGameID()))
                    {
                        if(lst.get(i).getTongSoBit()>0)
                        {
                            double iARPU=(lst.get(i).getTongSoBit() * 2 / 100)/(lst.get(i).getSoNguoiChoiDistinct()<=0?1:lst.get(i).getSoNguoiChoiDistinct());
                            l.set(lst.get(i).getThang(), iARPU);
                            bln=true;
                            //l.setShowLine(true);
                        }
                    }
                   
                        
                    i++;
                }
                if(bln)
                    c.addSeries(l);
            }
            
           
        }
        else
        {
           
            LineChartSeries l2=new LineChartSeries();
            l2.setLabel("Tổng số tiền nạp theo CP");

            for (RPT_TongDoanhThuTheoGameModel m : lst) {
               
                l2.set(m.getNgay(),m.getTongSoBit());
                l2.setShowLine(true);    

            }
             
             c.addSeries(l2);
        }
        
        return c;
    }
     
   
       public CartesianChartModel RPT_ThongKeDoanhThu_Chart_CP0_line_ngay() throws SQLException {
       CartesianChartModel c = new CartesianChartModel();
       
        
        List<RPT_TongDoanhThuTheoGameModel> lst;

   

        WEBParamCollection Params = new WEBParamCollection();
        Params.add(("-1".equals(this.getCpid()) ? "" : this.getCpid()));
        Params.add(("-1".equals(this.getGameID()) ? "" : this.getGameID()));
        Params.add(this.getTuNgay());
        Params.add(this.getDenNgay());
        lst = DAL.executeQuery("{call RPT_TongDoanhThuTheoGame_Chart_line_ngay_CP0(?,?,?,?)}", Command.sqlStoredProcedure, Params, RPT_TongDoanhThuTheoGameModel.class);

        if(Sys_NguoiDungController.findUser().isIsAdmin())
        {
             List<RPT_TongDoanhThuTheoGameModel> lst1=DAL.executeQuery("Select distinct GameInfo.GameID,GameInfo.GameName from GameInfo order by GameInfo.GameID desc",RPT_TongDoanhThuTheoGameModel.class);
            for(RPT_TongDoanhThuTheoGameModel m:lst1)
            {
                 LineChartSeries l=new LineChartSeries();
                 
                   int i=0;
                boolean bln=false;
                
                //ChartSeries data=new ChartSeries();
                
                while(i<lst.size())
                {
                   
                    if(i==0)
                        l.setLabel("Game " + m.getGameName());
                    
                    if(!l.getData().containsKey(Utilities.parseDateTime(lst.get(i).getNgay(), "dd/MM")))
                    {
                        l.set(Utilities.parseDateTime(lst.get(i).getNgay(), "dd/MM"), 0);
                        l.setShowLine(true);
                    }
                    if(lst.get(i).getGameID()== null ? m.getGameID()== null : lst.get(i).getGameID().equals(m.getGameID()))
                    {
                        if(lst.get(i).getTongSoBit()>0)
                        {
                               double iARPU=(lst.get(i).getTongSoBit() * 2 / 100)/(lst.get(i).getSoNguoiChoiDistinct()<=0?1:lst.get(i).getSoNguoiChoiDistinct());
                               l.set(Utilities.parseDateTime(lst.get(i).getNgay(), "dd/MM"), iARPU);
                               bln=true;
                               //l.setShowLine(true);
                         }
                    }
                        
                    i++;
                }
                if(bln)
                    c.addSeries(l);
            }
                
              
            
           
        }
        else
        {
           
            LineChartSeries l2=new LineChartSeries();
            l2.setLabel("Tổng số tiền nạp theo CP");

            for (RPT_TongDoanhThuTheoGameModel m : lst) {
               
                l2.set(Utilities.parseDateTime(m.getNgay(), "dd/MM"),m.getTongSoBit());
                l2.setShowLine(true);    

            }
             
             c.addSeries(l2);
        }
        
        return c;
    }
     public CartesianChartModel RPT_ThongKeDoanhThu_Chart_CP0_line_thang() throws SQLException {
       CartesianChartModel c = new CartesianChartModel();
       
        
        List<RPT_TongDoanhThuTheoGameModel> lst;

   

        WEBParamCollection Params = new WEBParamCollection();
        Params.add(("-1".equals(this.getCpid()) ? "" : this.getCpid()));
        Params.add(("-1".equals(this.getGameID()) ? "" : this.getGameID()));
        Params.add(this.getTuNgay());
        Params.add(this.getDenNgay());
        lst = DAL.executeQuery("{call RPT_TongDoanhThuTheoGame_Chart_line_thang_CP0(?,?,?,?)}", Command.sqlStoredProcedure, Params, RPT_TongDoanhThuTheoGameModel.class);

        if(Sys_NguoiDungController.findUser().isIsAdmin())
        {
             List<RPT_TongDoanhThuTheoGameModel> lst1=DAL.executeQuery("Select distinct GameInfo.GameID,GameInfo.GameName from GameInfo order by GameInfo.GameID desc",RPT_TongDoanhThuTheoGameModel.class);
            for(RPT_TongDoanhThuTheoGameModel m:lst1)
            {
                 LineChartSeries l=new LineChartSeries();
                
                
                int i=0;
                boolean bln=false;
                
                //ChartSeries data=new ChartSeries();
                
                while(i<lst.size())
                {
                    if(i==0)
                        l.setLabel("Game " + m.getGameName());
                    
                    if(!l.getData().containsKey(lst.get(i).getThang()))
                    {
                        l.set(lst.get(i).getThang(), 0);
                        l.setShowLine(true);
                    }
                    if(lst.get(i).getGameID()== null ? m.getGameID()== null : lst.get(i).getGameID().equals(m.getGameID()))
                    {
                        if(lst.get(i).getTongSoBit()>0)
                           {
                               double iARPU=(lst.get(i).getTongSoBit() * 2 / 100)/(lst.get(i).getSoNguoiChoiDistinct()<=0?1:lst.get(i).getSoNguoiChoiDistinct());
                               l.set(lst.get(i).getThang(), iARPU);
                               bln=true;
                               //l.setShowLine(true);
                           }
                    }
                    i++;
                }
                if(bln)
                    c.addSeries(l);
            }
           
            
           
        }
        else
        {
           
            LineChartSeries l2=new LineChartSeries();
            l2.setLabel("Tổng số tiền nạp theo CP");

            for (RPT_TongDoanhThuTheoGameModel m : lst) {
               
                l2.set(m.getNgay(),m.getTongSoBit());
                l2.setShowLine(true);    

            }
             
             c.addSeries(l2);
        }
        
        return c;
    }
     

    public void cmd_RPT_ThongKeDoanhThu_Excel_Click(ActionEvent event) throws SQLException, ParseException {
        Map mapParams = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        this.setCpid(String.valueOf(mapParams.get("CPID")));
        this.setGameID(String.valueOf(mapParams.get("Game")));
        this.setLoaiThongKe(String.valueOf(mapParams.get("LoaiTK")));
        String strTuNgay = String.valueOf(mapParams.get("TuNgay"));
        String strDenNgay = String.valueOf(mapParams.get("DenNgay"));
        setTuNgay(Utilities.parseDate(strTuNgay, "dd/MM/yyyy"));
        setDenNgay(Utilities.parseDate(strDenNgay, "dd/MM/yyyy"));
        
        WEBParamCollection Params = WEBParamCollection.getInstance();
        Params.add(("-1".equals(this.getCpid()) ? "" : this.getCpid()));
        Params.add(("-1".equals(this.getGameID()) ? "" : this.getGameID()));
        Params.add(this.getTuNgay());
        Params.add(this.getDenNgay());
        List<RPT_TongDoanhThuTheoGameModel> lst = DAL.executeQuery("{call RPT_TongDoanhThuTheoGame(?,?,?,?)}", Command.sqlStoredProcedure, Params, RPT_TongDoanhThuTheoGameModel.class);

        setFileExporter(controller.thongDoanhThuTheoGameToExcel(tuNgay, denNgay, lst));
        if (getFileExporter() != null) {
            try {
                Utilities.deleteFile(new File(controller.getLocalPath() + "/" + getFileExporter().getName()));
            } catch (IOException ex) {
                Logger.getLogger(ReportController.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void cmd_RPT_ThongKeDoanhThu_ThongKe_Click(ActionEvent event) throws SQLException, ParseException {
        Map mapParams = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        this.setCpid(String.valueOf(mapParams.get("CPID")));
        this.setGameID(String.valueOf(mapParams.get("Game")));
        this.setLoaiThongKe(String.valueOf(mapParams.get("LoaiTK")));
        String strTuNgay = String.valueOf(mapParams.get("TuNgay"));
        String strDenNgay = String.valueOf(mapParams.get("DenNgay"));
        setTuNgay(Utilities.parseDate(strTuNgay, "dd/MM/yyyy"));
        setDenNgay(Utilities.parseDate(strDenNgay, "dd/MM/yyyy"));
        this.setPage("/layout/Web/thongkedoanhthutheogame.xhtml");
    }

    public void cmd_RPT_ThongKeDoanhThu_Chart_Click(ActionEvent event) throws SQLException, ParseException {
        Map mapParams = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        this.setCpid(String.valueOf(mapParams.get("CPID")));
        this.setGameID(String.valueOf(mapParams.get("Game")));
        this.setLoaiThongKe(String.valueOf(mapParams.get("LoaiDT")));
        String strTuNgay = String.valueOf(mapParams.get("TuNgay"));
        String strDenNgay = String.valueOf(mapParams.get("DenNgay"));
        setTuNgay(Utilities.parseDate(strTuNgay, "dd/MM/yyyy"));
        setDenNgay(Utilities.parseDate(strDenNgay, "dd/MM/yyyy"));
        //this.setPage("/layout/Chart/thongkedoanhthu.xhtml");
         this.setLoaiBD(String.valueOf(mapParams.get("Loai")));
        
         switch (this.getLoaiBD()) {
            case "2":
                this.setPage("/layout/Chart/thongkedoanhthutheogame.xhtml");
                break;
            case "4":
                this.setPage("/layout/Chart/thongkedoanhthutheogame_line_ngay.xhtml");
                break;
            case "5":
                this.setPage("/layout/Chart/thongkedoanhthutheogame_line_thang.xhtml");
                break;
        }
    }

    public List<RPT_TongDoanhThuTheoGameModel> RPT_TongDoanhThu() throws SQLException {
        List<RPT_TongDoanhThuTheoGameModel> lst;
        WEBParamCollection Params = new WEBParamCollection();
        Params.add(this.getCpid());
        Params.add(this.getGameID());
        Params.add(this.getLoaiThongKe());
        Params.add(this.getGiaTriTheNap());
        Params.add(this.getTuNgay());
        Params.add(this.getDenNgay());
        lst = DAL.executeQuery("{call RPT_TongDoanhThu(?,?,?,?,?,?)}", Command.sqlStoredProcedure, Params, RPT_TongDoanhThuTheoGameModel.class);

        return lst;

    }

    /**
     * @return the tong5
     */
    public long getTong5() {
        return tong5;
    }

    /**
     * @param tong5 the tong5 to set
     */
    public void setTong5(long tong5) {
        this.tong5 = tong5;
    }

    /**
     * @return the fileExporter
     */
    public StreamedContent getFileExporter() {
        return fileExporter;
    }

    /**
     * @param fileExporter the fileExporter to set
     */
    public void setFileExporter(StreamedContent fileExporter) {
        this.fileExporter = fileExporter;
    }

}
